using System;
using System.Runtime.InteropServices;
using System.Security.Permissions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Security;
using Microsoft.SharePoint.Administration;

namespace JobTillKm6_v2_App18.Features.JobTillKm6_v2
{

    /// <summary>
    /// This class handles events raised during feature activation, deactivation, installation, uninstallation, and upgrade.
    /// </summary>
    /// <remarks>
    /// The GUID attached to this class may be used during packaging and should not be modified.
    /// </remarks>

    [Guid("893ab70e-5e93-42c7-9d7d-c84a8aef95fb")]
    public class JobTillKm6_v2EventReceiver : SPFeatureReceiver
    {

        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            using (SPSite site = properties.Feature.Parent as SPSite)
            {
                // delete preview job item
                foreach (SPJobDefinition job in site.WebApplication.JobDefinitions)
                {
                    if (
                           job.Name == "JobTillKm6_v1"
                        || job.Name == "JobTillKm6_v2"
                        )
                    {
                        job.Delete();
                    }
                }
                JobTillKm6Process_App18 listLoggerJob =
                    new JobTillKm6Process_App18("JobTillKm6_v2", site.WebApplication);
                //SPDailySchedule schedule = new SPDailySchedule();
                //schedule.BeginHour = 1;
                //schedule.BeginMinute = 0;
                //schedule.BeginSecond = 0;
                //schedule.EndHour = 1;
                //schedule.EndMinute = 59;
                //schedule.EndSecond = 59;

                SPHourlySchedule schedule = new SPHourlySchedule();
                schedule.BeginMinute = 0;
                schedule.EndMinute = 59;

                listLoggerJob.Schedule = schedule;
                listLoggerJob.Update();
            }
        }

        public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        {
            using (SPSite site = properties.Feature.Parent as SPSite)
            {
                foreach (SPJobDefinition job in site.WebApplication.JobDefinitions)
                {
                    if (job.Name == "JobTillKm6_v1"
                        || job.Name == "JobTillKm6_v2"
                        )
                    {
                        job.Delete();
                    }
                }
            }
        }


        // Uncomment the method below to handle the event raised after a feature has been installed.

        //public override void FeatureInstalled(SPFeatureReceiverProperties properties)
        //{
        //}


        // Uncomment the method below to handle the event raised before a feature is uninstalled.

        //public override void FeatureUninstalling(SPFeatureReceiverProperties properties)
        //{
        //}

        // Uncomment the method below to handle the event raised when a feature is upgrading.

        //public override void FeatureUpgrading(SPFeatureReceiverProperties properties, string upgradeActionName, System.Collections.Generic.IDictionary<string, string> parameters)
        //{
        //}
    }
}
